import urlparse
import urllib
from base import Base
from urldb.model.url import *
from urldb.model.coordinate import Coordinate

''' 
'''
class Populate(Base):
    ignoreList = None

    '''
        don't save parameters in the ignore list
    '''
    def set_ignore_list(self, list):
        self.ignoreList = list

    '''
        add the url to the db
    '''
    def new_url(self, raw_url):
        # parse url
        p = urllib.unquote_plus(raw_url)
        p = urlparse.urlparse(p)
        qs = urlparse.parse_qs(p.query)

        session = self.get_session()
        session.query(Url)
        session.flush()

        # new url object
        url = Url()
        url.url = raw_url
        #url.host = p.hostname()

        # process the url params
        for i in qs:
            name = i
            val = qs[i][0]

            # convert X,Y (Oregon State Plane North) to lat,lon
            if Coordinate.looksLikeStatePlaneCoordinate(val):
                c = Coordinate(val);
                val = c.getLonLat()

            if self.ignoreList != None:
                if name in self.ignoreList or val in self.ignoreList:
                    continue

            param = UrlParam()
            param.name = name
            param.value = val
            url.params.append(param)

        # save to db
        session.add(url)
        session.commit();
        session.flush()
